package org.redswallow.filter.dispatch;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.redswallow.filter.plugin.EventSink;
import org.redswallow.log.LogFactory;
import org.redswallow.util.config.IPropertySet;
import org.slf4j.Logger;

public class PluginInvokerImpl implements PluginInvoker {

	private EventSink[] _pluginSinks;
	//private IPropertySet _props;
	
	private Logger log = LogFactory.getLog(PluginInvokerImpl.class); 
	
	public PluginInvokerImpl(EventSink[] eventSinks, IPropertySet props) {
		_pluginSinks = eventSinks;
		//_props = props;
	}
	
	
	@Override
	public boolean invoke(ServletRequest request, ServletResponse response) {
		
		//TODO:根据配置进行过滤
		
		//
		for(int i=0; i<_pluginSinks.length; ++i) {
			try{
				if (!_pluginSinks[i].invoke(request, response))
					return false;
			}catch(Exception e) {
				log.error("plugin '"+_pluginSinks[i].getClass().getName()+"' cannot invoke.", e);
			}
		}
		
		return true;
	}

	@Override
	public void initialize() {
		for(int i=0; i<_pluginSinks.length; ++i) {
			try{
				_pluginSinks[i].initialize();
			}catch(Exception e) {
				log.error("plugin '"+_pluginSinks[i].getClass().getName()+"' cannot initialize.", e);
			}
		}
	}

	@Override
	public void shutdown() {
		for(int i=0; i<_pluginSinks.length; ++i) {
			try{
				_pluginSinks[i].shutdown();
			}catch(Exception e) {
				log.error("plugin '"+_pluginSinks[i].getClass().getName()+"' cannot shutdown.", e);
			}
		}
	}



}
